/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.db;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * <p>Project: jdemo202203 - DbMetaDataDemo
 * <p>Powered by webrx On 2022-11-03 10:44:16
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class DbMetaDataDemo {
    Connection conn = null;

    @Test
    void db(){
        try{
            DatabaseMetaData dbmd = conn.getMetaData();

            //数据库 名称
            System.out.println(conn.getCatalog());

            //数据库系统产品名称 MySQL
            System.out.println(dbmd.getDatabaseProductName());
            //数据库系统版本号 6.5.51
            System.out.println(dbmd.getDatabaseProductVersion());

            //驱动名称
            System.out.println(dbmd.getDriverName());
            System.out.println(dbmd.getURL());
            System.out.println(dbmd.getUserName());

            System.out.println(dbmd.getDriverVersion());
            System.out.println(dbmd.getDriverMajorVersion());
            System.out.println(dbmd.getDriverMinorVersion());


            //JDBC 4.2
            System.out.println(dbmd.getJDBCMajorVersion());
            System.out.println(dbmd.getJDBCMinorVersion());


            PreparedStatement pst = conn.prepareStatement("show databases");
            ResultSet rs = pst.executeQuery();
            while(rs.next()){
                //System.out.println(rs.getString(1));
                String dn = rs.getString(1);
                System.out.println("数据库："+dn);
                System.out.println("-".repeat(30));
                PreparedStatement pt = conn.prepareStatement("show tables from "+dn);
                ResultSet rst = pt.executeQuery();
                while(rst.next()){
                    System.out.println(rst.getString(1));
                }
                rst.close();
                pt.close();
                System.out.println("-".repeat(30));
                System.out.println();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @BeforeEach
    void connection() {
        try {
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
            Properties prop = new Properties();
            prop.load(is);
            String driver = prop.getProperty("db.driver", "com.mysql.cj.jdbc.Driver");
            String url = prop.getProperty("db.url", "jdbc:mysql:/");
            String user = prop.getProperty("db.username", "root");
            String password = prop.getProperty("db.password", "");

            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterEach
    void close() {
        try {
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
